using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SimRL
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont testFont;
        Texture2D dude;
        Texture2D floor;
        Texture2D wall;
        Texture2D statue;
        Common.IMap m_Map;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // TODO: use this.Content to load your game content here
            testFont = Content.Load<SpriteFont>("Testing");
            dude = Content.Load<Texture2D>("human_m");
            wall = Content.Load<Texture2D>("dngn_rock_wall_08");
            floor = Content.Load<Texture2D>("floor_of_a_room");
            statue = Content.Load<Texture2D>("dngn_silver_statue");
            m_Map = GetMap();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            // TODO: Add your drawing code here
            spriteBatch.Begin();
            // spriteBatch.DrawString(testFont, "Hello World!", new Vector2(100, 100), Color.Black);

            foreach (uint i in m_Map.Tiles.Keys)
            {
                foreach (Common.ITile t in m_Map.Tiles[i])
                {
                    if (t.Type == Common.TileType.Floor)
                    {
                        spriteBatch.Draw(floor, t.Position, Color.White);
                    }
                    else if (t.Type == Common.TileType.Wall)
                    {
                        spriteBatch.Draw(wall, t.Position, Color.White);
                    }
                    else
                    {
                        spriteBatch.Draw(statue, t.Position, Color.Silver);
                    }
                }
            }
            spriteBatch.Draw(dude, new Rectangle(164, 164, 32, 32), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        protected Common.IMap GetMap()
        {
            IDictionary<uint, IList<Common.ITile>> d = new Dictionary<uint, IList<Common.ITile>>(2);
            d[0] = new List<Common.ITile>();
            d[1] = new List<Common.ITile>();
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(68, 68)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(100, 68)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(132, 68)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(164, 68)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(196, 68)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(68, 100)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(100, 100)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(132, 100)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(164, 100)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(196, 100)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(68, 132)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(100, 132)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(132, 132)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(164, 132)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(196, 132)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(68, 164)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(100, 164)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(132, 164)));
            d[0].Add(new Common.Tile(Common.TileType.Floor, (Common.Direction)Common.Constants.AllDirections, new Vector2(164, 164)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(196, 164)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(68, 196)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(100, 196)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(132, 196)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(164, 196)));
            d[0].Add(new Common.Tile(Common.TileType.Wall, (Common.Direction)Common.Constants.NoDirections, new Vector2(196, 196)));
            d[1].Add(new Common.Tile(Common.TileType.Feature, (Common.Direction)Common.Constants.AllDirections, new Vector2(132, 132)));
            Common.IMap m = new Common.Map(d);
            return m;
        }
    }
}
